package com.huanong.it.xwuser.authority.common.filter;


import com.huanong.it.xwuser.authority.common.entity.MyConst;
import com.huanong.it.xwuser.authority.common.exception.JwtExpiredException;
import com.huanong.it.xwuser.authority.common.utils.JwtUtil;
import com.huanong.it.xwuser.authority.common.utils.RedisUtil;
import com.huanong.it.xwuser.authority.domain.User;
import com.huanong.it.xwuser.authority.domain.model.LoginUser;
import com.huanong.it.xwuser.authority.service.UserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        boolean flag=false;
        String s= String.valueOf(request.getRequestURL());

        for (String url: MyConst.WHITE_LIST) {
            AntPathRequestMatcher matcher = new AntPathRequestMatcher(url);
            if (matcher.matches(request)) {
                flag = true;
                break;
            }

        }
        return flag;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String token=request.getHeader("Authorization");
        if(token!=null&&!token.isEmpty()){
            try {
                Claims claims = jwtUtil.parseJwt(token);
                //根据账号，获取详细的用户信息
                String username = claims.getSubject();

                User user = userService.getUserByUserName(username);
                LoginUser loginUser = new LoginUser();
                loginUser.setUser(user);


                UsernamePasswordAuthenticationToken userToken = new UsernamePasswordAuthenticationToken(loginUser, loginUser.getPassword(), loginUser.getAuthorities());
                WebAuthenticationDetails details = new WebAuthenticationDetailsSource().buildDetails(request);
                //设置验证细节
                userToken.setDetails(details);
                //设置安全上下文
                SecurityContextHolder.getContext().setAuthentication(userToken);
            } catch (ExpiredJwtException | JwtExpiredException e) {
                logger.error("token过期");

            }
        }
        filterChain.doFilter(request,response);
    }


}


